package com.spring.sunflower.util;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Handler;

import com.blankj.utilcode.util.ToastUtils;
import com.spring.sunflower.base.BaseApplication;

public class AudioUtils {
    private static final String TAG = "AudioUtil";

    private Handler mHandler = new Handler();

    private MediaRecorder mMediaRecorder;

    private MediaPlayer mPlayer;
    private static AudioUtils mInstance;

    public static AudioUtils getInstance() {
        if (mInstance == null) {
            synchronized (AudioUtils.class) {
                if (mInstance == null) {
                    mInstance = new AudioUtils();
                }
            }
        }
        return mInstance;
    }

    public void cancelRecord() {
        if (mMediaRecorder != null)
            try {
                mMediaRecorder.stop();
                mMediaRecorder.reset();
                mMediaRecorder.release();
                mMediaRecorder = null;
                return;
            } catch (Exception mediaRecorder) {
                mediaRecorder.printStackTrace();
            }
    }

    public boolean isPause() {
        return (mPlayer != null && !mPlayer.isPlaying());
    }

    public boolean isPlay() {
        return (mPlayer != null && mPlayer.isPlaying());
    }

    public void pause() {
        if (mPlayer != null)
            mPlayer.pause();
    }

    public void play(String paramString, final PlayCallBack callBack) {
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
        } else {
            mPlayer.reset();
        }
        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            public void onCompletion(MediaPlayer param1MediaPlayer) {
                AudioUtils.PlayCallBack playCallBack = callBack;
                if (playCallBack != null)
                    playCallBack.onCompletion(param1MediaPlayer);
            }
        });
        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            public boolean onError(MediaPlayer param1MediaPlayer, int param1Int1, int param1Int2) {
                AudioUtils.PlayCallBack playCallBack = callBack;
                if (playCallBack != null)
                    playCallBack.onCompletion(param1MediaPlayer);
                ToastUtils.showShort("音频播放错误:(");
                AudioUtils.this.playEndOrFail();
                return false;
            }
        });
        try {
            if (paramString.startsWith("http") || paramString.startsWith("https")) {
                mPlayer.setDataSource(BaseApplication.getContext(), Uri.parse(paramString));
            } else {
                mPlayer.setDataSource(paramString);
            }
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                public void onPrepared(MediaPlayer param1MediaPlayer) {
                    mPlayer.start();
                    AudioUtils.PlayCallBack playCallBack = callBack;
                    if (playCallBack != null)
                        playCallBack.onPrepared(param1MediaPlayer);
                }
            });
            mPlayer.prepareAsync();
            return;
        } catch (Exception e) {
            return;
        }
    }

    public void playEndOrFail() {
        if (mPlayer != null) {
            mPlayer.setOnCompletionListener(null);
            mPlayer.setOnErrorListener(null);
            mPlayer.stop();
            mPlayer.reset();
            mPlayer.release();
            mPlayer = null;
        }
    }

    public void resume() {
        if (mPlayer != null)
            mPlayer.start();
    }

    public void starRecord(String paramString) {
        try {
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(3);
            mMediaRecorder.setAudioEncoder(1);
            mMediaRecorder.setAudioSamplingRate(44100);
            mMediaRecorder.setAudioEncodingBitRate(16);
            mMediaRecorder.setOutputFile(paramString);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            return;
        } catch (Exception e) {
            cancelRecord();
            return;
        }
    }

    public void starRecordM4A(String paramString) {
        try {
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//            mMediaRecorder.setAudioSamplingRate(44100);
//            mMediaRecorder.setAudioEncodingBitRate(16);
            mMediaRecorder.setOutputFile(paramString);
            mMediaRecorder.prepare();
            mMediaRecorder.start();
            return;
        } catch (Exception e) {
            cancelRecord();
            return;
        }
    }

    public void stopPlay() {
        MediaPlayer mediaPlayer = this.mPlayer;
        if (mPlayer != null) {
            mPlayer.stop();
            mPlayer.release();
            mPlayer = null;
        }
    }

    public static interface PlayCallBack {
        void onCompletion(MediaPlayer param1MediaPlayer);

        void onPrepared(MediaPlayer param1MediaPlayer);
    }
}